package com.lsl.bhllserver.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.lsl.bhllserver.dao.OrderDAO;
import com.lsl.bhllserver.dao.ProductDAO;
import com.lsl.bhllserver.entity.Order;
import com.lsl.bhllserver.service.func.IOrderBulider;

@Service
public class OrderService {

    @Autowired
    protected OrderDAO orderDAO;

    @Autowired
    protected ProductDAO productDAO;

    public List<Order> searchOrder(String[] keywords) {
        if (keywords[4].equals("未发货")) {
            keywords[4] = "0";
        } else if (keywords[4].equals("已发货")) {
            keywords[4] = "1";
        } else if (keywords[4].equals("已送达")) {
            keywords[4] = "2";
        } else if (keywords[4].equals("已签收")) {
            keywords[4] = "3";
        } else if (keywords[4].equals("申请取消")) {
            keywords[4] = "4";
        } else if (keywords[4].isBlank()) {
            keywords[4] = "";
        } else {
            keywords[4] = "5";
        }
        return orderDAO.findByKeywords(keywords);
    }

    // @Cacheable(value = "order", key = "#oid")
    public Order getOrder(String oid) {
        return orderDAO.findByID(oid);
    }

    @CacheEvict(value = "allOrder", key = "'allOrder'")
    public boolean deleteOrder(String oid) {
        return orderDAO.delete(Integer.parseInt(oid));
    }

    @CacheEvict(value = "cusMyOrder", key = "#ob.build().getCusId()", condition = "#ob.build().getCusId() != null")
    public boolean problemOrder(IOrderBulider ob) {
        Order temp = orderDAO.findByID(ob.build().getId().toString());
        temp.setOrderStatus(4);
        return orderDAO.update(temp.getId(), temp);
    }
    
}
